package com.example.mapp;

import com.example.entity.RepairInfo;
import com.example.ext.entity.ExtRepairInfo;
import com.example.mapp.buildSql.RepairInfoMappSql;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2017/9/22.
 */
@Mapper
public interface RepairInfoMapp {

    @Select(" select  r.*, " +
            "s.user_name as repairUserName ," +
            "ss.user_name as confirmUserName " +
            "from repair_info r " +
            "LEFT join sys_user s on s.id = r.repair_user_id " +
            "LEFT join sys_user ss on ss.id = r.confirm_user_id  where r.id=#{id}")
    ExtRepairInfo queryById(String id) throws Exception;

    @Delete("delete from repair_info where id=#{id}")
    int deleteById(String id)throws Exception;

    @Insert("insert into repair_info " +
            "(report_date,resolve_date,elevator_num,elevator_id,fault_description,reason," +
            "replace_parts,repair_user_id,confirm_user_id,remarks, repair_party_id,apply_repair_id, maint_sign_url,repair_cost,unrepaired_cause) " +
            "value " +
            "(#{reportDate},#{resolveDate},#{elevatorNum},#{elevatorId},#{faultDescription}," +
            "#{reason},#{replaceParts},#{repairUserId},#{confirmUserId},#{remarks},#{repairPartyId},#{applyRepairId},#{maintSignUrl},#{repairCost},#{unrepairedCause}) ")
    @SelectKey(statement="select last_insert_id()",keyProperty = "id",keyColumn = "id",before = false,resultType = Long.class)
    int add(RepairInfo repairInfo)throws Exception;

    @Update("update repair_info set is_repaired=#{isRepaired},unrepaired_cause = #{unrepairedCause}, safe_sign_url=#{safeSignUrl} ,confirm_user_id=#{confirmUserId},resolve_date=#{resolveDate} where id=#{id}")
    int update(RepairInfo repairInfo) throws Exception;

    @SelectProvider(type = RepairInfoMappSql.class,
            method = "queryByElevatorList")
    List<ExtRepairInfo> queryByElevatorList(RepairInfo repairInfo) throws Exception;

    @SelectProvider(type = RepairInfoMappSql.class,
            method = "countByElevatorList")
    int countByElevatorList(RepairInfo repairInfo) throws Exception;

    @Select(" select a.*, b.user_name as repairUserName, c.user_name as confirmUserName from repair_info a " +
            " left join elevator s on a.elevator_num=s.elevator_num " +
            " left join sys_user b on a.repair_user_id=b.id " +
            " left join sys_user c on a.confirm_user_id=c.id " +
            " where s.used_party_id=#{partyId} and (a.is_repaired='0' or a.is_repaired is null) order by a.resolve_date desc ")
    List<ExtRepairInfo> queryUsedRepairList(Map<String, Object> inMap) throws Exception;

    @Select("select * from repair_info where id = #{repairInfoId}")
    ExtRepairInfo queryRepairInfoById (Map<String, Object> inMap) throws Exception;

    @Select("select * from repair_info where elevator_num = #{elevatorNum} and resolve_date = #{resolveDate}")
    ExtRepairInfo findRepairInfoId (Map<String, Object> inMap) throws Exception;
}
